package com.hxh.juc;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/11 21:23
 */
class A{
    int flag = 0;
    public synchronized void sou1() throws InterruptedException {
        while (flag!=0){
            Thread.sleep(1000);
            System.out.println(123);
        }

        System.out.print("A");
        flag = 1;
        this.notifyAll();
    }


    public synchronized void sou2() throws InterruptedException {
        while (flag!=1){
            Thread.sleep(100);
            System.out.println(234);
        }

        System.out.print("B");
        flag = 2;
        this.notifyAll();
    }


    public synchronized void sou3() throws InterruptedException {
        while (flag!=2){
            //this.wait();
            Thread.sleep(10);
            System.out.println(456);
        }

        System.out.println("C");
        flag = 0;
        this.notifyAll();
    }


}
public class Test {
    public static void main(String[] args) {
        A a = new A();
        new Thread(()->{
          for(int i = 0;i<=5;i++){
              try {
                  a.sou1();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
        }).start();

        new Thread(()->{
            for(int i = 0;i<=5;i++){
                try {
                    a.sou2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            for(int i = 0;i<=5;i++){
                try {
                    a.sou3();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
